//登录拦截器

const { redis } = require('../config/env');
const RedisStore = require('koa-redis')(redis);
const bodyparser = require('koa-bodyparser');

async function stopping(ctx, next) {
    let session_id = ctx.cookies.get('session_id');
    if (session_id) {
        let result = RedisStore.get(session_id);
        if (result.logged !== false) {
            if (ctx.request.originalUrl !== '/v1/user/login') {
                ctx.redirect('/home');       //尚不明确用户个人界面路由，不知道该重定向至哪个路由，先用home代替
                await next();
            } else {
                await next();
            }
        } else {
            ctx.body = {
                ErrorInfo: '请先登录!',
                statusCode: 500
            }
            await next();
        }
    } else {
        ctx.body = {
            ErrorInfo: '请先登录!',
            statusCode: 500
        }
        await next();
    }
}

module.exports = stopping;